<?php
    include_once('../src/classes/class.mysql.php');
    
    session_start();
    
    $comtec_id_mainteiner = $_GET["id"];
    $comtec_id_user       = $_SESSION["comtec_id_user"];
    
    $o                   = isset($_GET["o"])? $_GET["o"] : "";
    $s                   = isset($_GET["s"])? $_GET["s"] : "";
    
    if($o == "" && $s == ""){
     $op = "";   
    }
    
    if($o == "1" && $s == "1"){
     $op = "1";   
    }
    if($o == "1" && $s == "0"){
     $op = "2";   
    }
    if($o == "2" && $s == "1"){
     $op = "3";   
    }
    if($o == "2" && $s == "0"){
     $op = "4";   
    }
    
    $db = new MySQL();
    
    $tsql = "  SELECT (SELECT op.operation_date_end
                        FROM operation as op
                        WHERE op.comtec_id_machine = m.comtec_id_machine AND op.status_reg=1
                        ORDER BY op.operation_date_end DESC LIMIT 1) AS last_operation,o.company_name,m.comtec_id_machine, m.cod_mainteiner, m.brand, m.model, m.year, m.date_next_maintenance AS date ";  
    if ($_SESSION["users"]!="clients"){
     $tsql .= " FROM machines as m INNER JOIN owner as o ON m.comtec_id_owner = o.comtec_id_owner 
               WHERE m.comtec_id_mainteiner=$comtec_id_mainteiner and m.status_reg=1 ";      
    }else{
     $tsql .= " FROM machines as m INNER JOIN owner as o ON m.comtec_id_owner = o.comtec_id_owner 
               WHERE o.comtec_id_user=$comtec_id_user AND m.comtec_id_mainteiner=$comtec_id_mainteiner and m.status_reg=1 "; 
    }
    
    switch($op){
     case 1: $tsql .= " ORDER BY company_name ASC ";
             $s = 0;
     break;
     case 2: $tsql .= " ORDER BY company_name DESC ";
             $s = 1;
     break;
     case 3: $tsql .= " ORDER BY date ASC ";
             $s = 0;
     break;
     case 4: $tsql .= " ORDER BY date DESC ";
             $s = 1;
     break;
     default : $tsql .= " ORDER BY date ASC ";
               $s = 1;
    }
    
    $stmt = $db->query($tsql);
    
    $url_arrow="";
    if ($s == 1 && $op != ""){
    $url_arrow='http://'.$_SERVER['HTTP_HOST'].'/iconos/arrow_up.png';
    }elseif($s == 0 && $op != "") 
        {
         $url_arrow='http://'.$_SERVER['HTTP_HOST'].'/iconos/arrow_down.png';
        }
    
    $texto  = "<table class='table'>";
    if(($_SESSION["users"]!="clients") && ($_SESSION["users"]!="administratives-2")){     
     $texto .= "<tr><th colspan=2></th>";
    }else{$texto .= "<tr><th></th>";}          
    $texto .= "<th>C&oacute;digo</th>
              <th>Marca</th>
              <th>Modelo</th>
              <th>A&ntilde;o</th>
              <th><a href='javascript:ordenarMaquinas(".$s.",1);'>Propietario
              <img src='$url_arrow' WIDTH=15 HEIGHT=20 />
              </a>
              </th>
              <th>Fecha ult. oper.</th>
              <th><a href='javascript:ordenarMaquinas(".$s.",2);'>Fecha pr&oacute;x. oper.</a></th>             
              </tr>";  
    $cont = 0;
    
    while ($row = mysql_fetch_array($stmt, MYSQL_ASSOC)){
     $cont += 1;
     $comtec_id_machine=$row['comtec_id_machine'];
     if($row['last_operation'] != null){
     $last_operation = $row['last_operation']; 
     }else {
     $last_operation = '';
     }
     $cod_mainteiner=$row['cod_mainteiner'];
     $brand=$row['brand'];
     $model=$row['model'];
     $year=$row['year'];
     $propietario=$row['company_name'];
     $texto .= "<tr>";
     //ver ordenes
     $url_view='http://'.$_SERVER['HTTP_HOST'].'/iconos/viewOrders.png';
     $texto .= "<td><a href='javascript:loadOrders(".$comtec_id_machine.");'><img src='$url_view' alt='Ver ordenes' title='Ver ordenes de trabajo' style='border: 0px;' /></a></td>";     
     //agregar orden
     $url_add='http://'.$_SERVER['HTTP_HOST'].'/iconos/addOrder.png';
     $texto .= "<td><a href='javascript:addOrder(".$comtec_id_machine.");'><img src='$url_add' alt='Ingresar orden' title='Ingresar orden de trabajo' style='border: 0px;' /></a></td>";
     $texto .= "<td><a href='javascript:machine(".$comtec_id_machine.");'>$cod_mainteiner</a></td>";
     $texto .= "<td>$brand</td>";
     $texto .= "<td>$model</td>";
     $texto .= "<td>$year</td>";
     $texto .= "<td>$propietario</td>";
     $texto .= "<td>$last_operation</td>";
     $texto .= "<td>$date</td>";
     $texto .= "</tr>";
    }
    $texto .= "</table>";
    $texto .= '<div id="result"></div>';
    echo "<p class='total'>Total: $cont</p>".$texto;
?>
